package com.xwaf.platform.system.provider.serviceimpl.pay;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.xwaf.platform.common.utils.annotation.Compensable;
import com.xwaf.platform.system.api.entity.pay.Order;
import com.xwaf.platform.system.api.exception.BusinessException;
import com.xwaf.platform.system.api.service.auth.AccountService;
import com.xwaf.platform.system.api.service.pay.PaymentService;
import com.xwaf.platform.system.provider.dao.pay.OrderDao;

@Transactional(readOnly = true)
@Service("paymentService")
public class PaymentServiceImpl implements PaymentService {

	private static final Logger log = LoggerFactory.getLogger(PaymentServiceImpl.class);

	@Autowired
	AccountService accountService;
	@Autowired
	private OrderDao orderDao;

	@Compensable(confirmMethod = "paymentConfirm", cancelMethod = "paymentCancel")
	@Override
	public void payment(Order saveOrder) {
		log.info("==================payment开始=======================");
		// 扣减库存
		// 扣减账户余额
		Integer decreaseAccount = accountService.decreaseAccount(saveOrder);
		if (decreaseAccount == null || decreaseAccount == 0) {
			throw new RuntimeException("decreaseAccount error");
		}
		log.info("==================payment结束=======================");
	}

	/**
	 * paymentConfirm 失败时需要抛出异常
	 * 
	 * @throws BusinessException
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void paymentConfirm(String orderId) throws BusinessException {
		log.info("==================paymentConfirm开始=======================");
		Order dbOrder = orderDao.selectById(orderId);
		if (dbOrder != null) {
			dbOrder.setStatus("2");
			dbOrder.setUpdateTime(new Date());
			int influence = orderDao.updateById(dbOrder);
			if (influence < 1) {
				throw new BusinessException("paymentConfirm exception");
			}
		} else {
			log.warn("confirm找不到订单，req:{}", orderId);
		}
		log.info("==================paymentConfirm结束=======================");
	}

	/**
	 * paymentCancel 失败时需要抛出异常
	 * 
	 * @throws BusinessException
	 */
	@Transactional(readOnly = false, isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public void paymentCancel(String orderId) throws BusinessException {
		log.info("==================paymentCancel开始=======================");
		Order dbOrder = orderDao.selectById(orderId);
		if (dbOrder != null) {
			dbOrder.setStatus("3");
			dbOrder.setUpdateTime(new Date());
			int influence = orderDao.updateById(dbOrder);
			if (influence < 1) {
				throw new BusinessException("paymentCancel exception");
			}
		} else {
			log.warn("cancel找不到订单，req:{}", orderId);
		}
		log.info("==================paymentCancel结束=======================");
	}
}
